import pygame
import sys

WHITE = pygame.Color('white')
BLACK = pygame.Color('black')


class Building:

    def __init__(self):
        pygame.init()
        self.winx = 300
        self.winy = 600
        self.screen = pygame.display.set_mode((self.winx, self.winy))
        self.screen.fill(WHITE)
        pygame.display.set_caption('FOR ANN')
        fxy = [55, self.winy - 30]
        fspeed = 1
        fstr = '欢迎美冉回家'
        fclock = pygame.time.Clock()
        while True:
            for e in pygame.event.get():
                if e.type == pygame.QUIT:
                    pygame.quit()
                    sys.exit()
            self.screen.fill((255, 255, 255))
            if fxy[1] + 700 < 0:
                fxy[1] = self.winy - 30
            fxy[1] -= fspeed
            for index, str in enumerate(fstr):
                # fxy[1] -= index*100
                self.show_str(str, 0, (fxy[0], fxy[1] + index * 150), 170, (0, 255, 255))
            # self.show_str(fstr[1], 0, (fxy[0],fxy[1]+100), 170, (0, 255, 255))

            # print(str, index, fxy)
            self.show_building()
            self.show_windows(6, 10)
            pygame.display.update()
            fclock.tick(40)

    def show_str(self, str, direct, xy_tuple, size, color):
        str_list = list(str)
        font = pygame.font.SysFont('SimHei', size)
        i = 0
        text = font.render(str_list[i], True, color)
        self.screen.blit(text, xy_tuple)

    def show_building(self, edge=30):
        pygame.draw.rect(self.screen, BLACK, (edge, edge, self.winx - edge * 2, self.winy - edge * 2), 2)

    def show_windows(self, row=2, col=5):
        edge = 30  # 楼和画面之间的距离
        distance = 10  # windows之间 windows与楼边缘的距离
        w = (self.winx - edge * 2 - distance * 2 - distance * (row - 1)) / row
        # if w < distance:
        #     w = distance
        h = (self.winy - edge * 2 - distance * 2 - distance * (col - 1)) / col
        # if h < distance:
        #     h = distance
        for i in range(row):
            for j in range(col):
                x = distance * (i + 1) + w * i + edge
                y = distance * (j + 1) + h * j + edge
                # print(f'x{x},y{y},w{w}.h{h}')
                pygame.draw.rect(self.screen, BLACK, (x, y, w, h), 2)


if __name__ == '__main__':
    b = Building()
